package com.tedu.controller;

import com.tedu.pojo.Information;
import com.tedu.pojo.Topic;
import com.tedu.servies.InforNewsServies;
import com.tedu.servies.TopicServies;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class NewsController {

    @Autowired
    InforNewsServies inforNewsServies;
    //声明并实例化 对象TopicServiesTopicServies
    @Autowired
    TopicServies topicServies;

    /**
     * 显示党建信息类型数据   分页显示  20条记录显示一页
     * @RequestParam(name = "topicID",defaultValue = "1",required = false)  name:参数名称；defaultValue：默认值；required：前端可以不用传入该参数
     * @return
     */
    @RequestMapping("/getNewsList")
    public String getNewsList(@RequestParam(name = "topicID",defaultValue = "1",required = false) int topicID,
                              @RequestParam(name = "currentpage",defaultValue = "1",required = false) int currentpage, Model model){
        System.out.println("你正在分类型查看"+currentpage);
        //调用服务层方法  获取党建信息
        List<Information> informationByType = inforNewsServies.getInformationByType(topicID,currentpage);
        model.addAttribute("newslist",informationByType);

        //通过topicID获取信息类型的名称，并发送到页面
        Topic topic =  topicServies.getTopicById(topicID);
        // 发送到页面
        model.addAttribute("topic",topic);
        //发送信息的类型id
        model.addAttribute("topicID",topicID);

        //调用服务层方法，计算页数  页面大小---20条记录是一页
        int totalpage = inforNewsServies.getCountByType(topicID);

        //从服务中取党建信息类型数据
        List<Topic> allTopic = topicServies.getTopic();
        model.addAttribute("allTopic",allTopic);


        model.addAttribute("currentpage",currentpage);
        model.addAttribute("totalpage",totalpage);
        return "forward:/WEB-INF/jsp/newslist.jsp";// 经过视图解析器  解析到一个视图

    }

    /**
     * 处理用户  党建信息阅读的  方法
     * @param inforId
     * @param model
     * @return
     */
    @RequestMapping("/readerNews")
    public String readNews(int inforId,Model model){
        System.out.println("你正在阅读新闻：id:"+inforId);
        //调用服务获取该党建信息的详情
        Information information = inforNewsServies.getInformationById(inforId);
        //发送信息到页面
        model.addAttribute("information",information);
        System.out.println(information);

        //调用服务方法  创建10个党建信息类型数据
        List<Topic> topic = topicServies.getTopic();
        //发送数据到页面  10个  随机
        model.addAttribute("topics",topic);
        // 发送该党建信息对应的类型
        Topic topicById = topicServies.getTopicById(information.getType());
        model.addAttribute("topicById",topicById);


        return "forward:/WEB-INF/jsp/news.jsp";
    }


}
